package com.ithuangyl.main;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ithuangyl.annotation.BindPath;
import com.ithuangyl.aroute.ARouter;
import com.ithuangyl.common.base.BaseActivity;
import com.ithuangyl.common.base.BaseFragment;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@BindPath("activity/main")
public class MainActivity extends BaseActivity {
    ViewPager viewPager;
    RadioGroup vRadioGroup;
    RadioButton vHomeRadio;
    RadioButton vFindRadio;
    RadioButton vMeRadio;

    List<BaseFragment> mFragments = new ArrayList<>();
    private BaseFragment homeFragment = null;
    private BaseFragment findFragment = null;
    private BaseFragment mineFragment = null;

    private void initFragment() {
        if (mFragments.size() > 0) {
            mFragments.clear();
        }
        homeFragment = (BaseFragment) ARouter.create().createFragment("fragment/home", null);
        findFragment = (BaseFragment) ARouter.create().createFragment("fragment/find", null);
        mineFragment = (BaseFragment) ARouter.create().createFragment("fragment/mine", null);
        if (homeFragment != null) {
            mFragments.add(homeFragment);
        }
        if (findFragment != null) {
            mFragments.add(findFragment);
        }
        if (mineFragment != null) {
            mFragments.add(mineFragment);
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        setStatusBarView(R.drawable.shape_main_bar_bg);
        statusBarDarkFont(false, 1.0f);
        viewPager = $(R.id.id_viewPager);
        vRadioGroup = $(R.id.id_radioGroup);
        vHomeRadio = $(R.id.id_radio_home);
        vFindRadio = $(R.id.id_radio_find);
        vMeRadio = $(R.id.id_radio_me);
        vTopBar.title("首页", R.color.white, null)
                .showLeftIcon(false)
                .setBackgroundResource(R.drawable.shape_main_bar_bg);
    }

    @Override
    public void initViewed() {
        initFragment();
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), 0) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
        vRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            View viewById = findViewById(checkedId);
            if (vHomeRadio.equals(viewById)) {
                viewPager.setCurrentItem(0, false);
            } else if (vFindRadio.equals(viewById)) {
                viewPager.setCurrentItem(1, false);
                if (homeFragment != null) {
                    Class<?> clazz = ARouter.create().getFragment("fragment/home");
                    try {
                        Method m = clazz.getMethod("doSomething");
                        m.setAccessible(true);
                        m.invoke(homeFragment);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else if (vMeRadio.equals(viewById)) {
                viewPager.setCurrentItem(2, false);
            }
        });
    }
}
